package com.zp.self.module.level_1_问道.X_JDK8新功能.日期时间新API;

import org.junit.Test;

import java.time.LocalDate;

/**
 * @author By ZengPeng
 * @Description
 * @date in  2021/3/2 16:21
 * @Modified By
 */
public class 创建时间_LocalDate {
    @Test
    public void test(){
        //--------------------------------1、创建时间对象--------------------------------
        //now ，静态方法，根据当前时间创建对象
        //of，静态方法，根据指定日期、时间创建对象
        //parse，静态方法，通过字符串指定日期
        LocalDate localDate1 = LocalDate.now();
        System.out.println(localDate1);  //2020-09-05
        LocalDate localDate2 = LocalDate.of(2020, 9, 5);
        System.out.println(localDate2); //2020-09-05
        LocalDate localDate3 = LocalDate.parse("2020-09-05");
        System.out.println(localDate3); //2020-09-05

        //--------------------------------2、获取年月日周--------------------------------
        //getYear，获取年
        //getMonth ，获取月份，返回的是月份的枚举值
        //getMonthValue，获取月份的数字（1-12）
        //getDayOfYear，获取一年中的第几天（1-366）
        //getDayOfMonth，获取一个月中的第几天（1-31）
        //getDayOfWeek，获取一周的第几天，返回的是枚举值
        LocalDate currentDate = LocalDate.now();
        System.out.println(currentDate.getYear()); //2020
        System.out.println(currentDate.getMonth()); // SEPTEMBER
        System.out.println(currentDate.getMonthValue()); //9
        System.out.println(currentDate.getDayOfYear()); //249
        System.out.println(currentDate.getDayOfMonth()); //5
        System.out.println(currentDate.getDayOfWeek()); // SATURDAY

        //--------------------------------3、日期比较，前后或者相等--------------------------------
        //isBefore ，第一个日期是否在第二个日期之前
        //isAfter，是否在之后
        //equals，日期是否相同
        //isLeapYear，是否是闰年

        LocalDate date1 = LocalDate.of(2020, 9, 5);
        LocalDate date2 = LocalDate.of(2020, 9, 6);
        System.out.println(date1.isBefore(date2)); //true
        System.out.println(date1.isAfter(date2)); //false
        System.out.println(date1.equals(date2)); //false
        System.out.println(date1.isLeapYear()); //true

        //--------------------------------3、日期比较，前后或者相等--------------------------------
        //plusDays, 加几天
        //plusWeeks, 加几周
        //plusMonths, 加几个月
        //plusYears，加几年
        LocalDate nowDate = LocalDate.now();
        System.out.println(nowDate);  //2020-09-05
        System.out.println(nowDate.plusDays(1)); //2020-09-06
        System.out.println(nowDate.plusWeeks(1)); //2020-09-12
        System.out.println(nowDate.plusMonths(1)); //2020-10-05
        System.out.println(nowDate.plusYears(1)); //2021-09-05
    }
}
